<?php
/**
 * Static content controller.
 *
 * This file will render views from views/posts/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/posts-controller.html
 */
class PostsController extends AppController {
    public $helpers = array('Html', 'Form', 'Paginator');

    public $paginate = array();

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('index', 'view');
    }

    public function index() {
        if (AppController::isAdmin()) {
            $this->paginate = array(
            'limit' => 5,
            'order' => array(
                'Post.created' => 'desc'),
            'field' => array('id', 'title', 'created')
            );
            $data = $this->paginate('Post');
        } else {
            $this->paginate = array(
            'limit' => 5,
            'order' => array(
                'Post.created' => 'desc'),
            'field' => array('id', 'title', 'created')
            );
            $data = $this->paginate('Post');
        }
		
        $this->set('posts', $data);
    }

    public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }

        $post = $this->Post->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->set('post', $post);
    }
    
    public function add() {
    	if ($this->request->is('post')) {
            $this->Post->create();
            $this->request->data['Post']['user_id'] = $this->Auth->user('id');
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('Your post has been saved.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to add your post.'));
        }
    }

    public function edit($id = null){
    	if (!$id) {
    		throw new NotFoundException(__('Invalid post'));
    	}
    	$post = $this->Post->findById($id);
        $this->set('post', $post);

    	if (!$post) {
    		throw new NotFoundException(__('Invalid post'));
    	}

    	if ($this->request->is(array('post', 'put'))) {
    		$this->Post->id = $id;
            $this->request->data['Post']['user_id'] = $this->Auth->user('id');
    		if ($this->Post->save($this->request->data)) {
    			$this->Session->setFlash(__('Your post has been update.'));
    			return $this->redirect(array('action' => 'view', $id));
    		}
    		$this->Session->setFlash(__('Unable to update your post.'));
    	}

    }

    public function delete($id){
    	if ($this->request->is('get')) {
    		throw new MethodNotAllowedException();
    	}

    	if ($this->Post->delete(($id))) {
    		$this->Session->setFlash(__('The post with id: %s has been deleted.', h($id)));
    		return $this->redirect(array('action' => 'index'));
    	}
    }
}